package com.example.chatplatform.controller;

import com.example.chatplatform.model.dto.AddCronyDTO;
import com.example.chatplatform.model.vo.CronyInformVO;
import com.example.chatplatform.result.Result;
import com.example.chatplatform.service.CronyService;
import com.example.chatplatform.ws.ChatEndpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/crony")
public class CronyController {

    @Autowired
    public CronyService cronyService;

    @Autowired
    public ChatEndpoint chatEndpoint;

    //查询好友状态
    @GetMapping("/cronyStatus")
    private Result cronyStatus(@RequestParam Integer cronyId) {
        if (chatEndpoint.onlineUser(cronyId))  {
            return Result.success("在线");
        } else {
            return Result.success("离线");
        }
    }

    //查询是否为好友
    @GetMapping("/isCrony")
    private Result isCrony(@RequestParam Integer userId, @RequestParam Integer cronyId) {
        if (cronyService.isCrony(userId,  cronyId))  {
            return Result.success(true);
        } else {
            return Result.success(false);
        }
    }

    //添加好友
    @PostMapping("/addCrony")
    private Result addCrony(@RequestBody AddCronyDTO addCrony) {
        if (cronyService.addCrony(addCrony)) {
            return Result.success("添加请求发送成功");
        } else {
            return Result.error("发送失败");
        }
    }

    //同意添加好友
    @GetMapping ("/agreeCrony")
    private Result agreeCrony(@RequestParam Integer cronyId, @RequestParam Integer userId) {
        if (cronyService.agreeCrony(cronyId, userId)) {
            return Result.success("已同意");
        } else {
            return Result.error("请求失败");
        }
    }

    //拒绝添加好友
    @GetMapping ("/refusalCrony")
    private Result refusalCrony(@RequestParam Integer cronyId, @RequestParam Integer userId) {
        if (cronyService.refusalCrony(cronyId, userId)) {
            return Result.success("已拒绝");
        } else {
            return Result.error("请求失败");
        }
    }

    //查询好友通知
    @GetMapping("/getCronyInform")
    private Result getCronyInform(@RequestParam Integer userId) {
        List<CronyInformVO> cronyInformVOS = cronyService.getCronyInform(userId);
        if (cronyInformVOS != null)  {
            return Result.success(cronyInformVOS);
        } else {
            return Result.success("查询失败");
        }
    }

    //删除好友
    @DeleteMapping("/deleteCrony")
    private Result deleteCrony(@RequestParam Integer userId, @RequestParam Integer cronyId) {
        if (cronyService.deleteCrony(userId, cronyId)) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

}
